<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../include/tag.jsp" %>
<%@ page import="com.party.core.model.system.TargetType" %>
<%@ taglib prefix="memberForm" uri="http://www.tongxingzhe.cn/memberForm" %>

<!DOCTYPE html>
<html lang="zh">
<head>
    <title>${project == null ? '发布' : (copy?'复制':'编辑')}项目</title>
    <%@include file="../include/commonFile.jsp" %>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/ui/activity/publish_form.css">
    <style>
        .add-form-content {
            max-width: inherit;
        }

        .add-form-content .form-inner {
            margin-right: 50px;
        }
    </style>
</head>
<body>
<!--头部-->
<%@include file="../include/header.jsp" %>
<div class="index-outside">
    <%@include file="../include/sidebar.jsp" %>
    <!--内容-->
    <section>
        <div class="section-main">
            <!-- 正文请写在这里 -->
            <div class="add-form-content" style="padding-right: 15px;">
                <form id="myForm" class="layui-form mt20" method="post" action="${ctx}/gatherForm/project/save.do">
                    <fieldset class="border mb10">
                        <legend>项目内容</legend>
                        <div class="form-inner">
                            <div class="layui-form-item">
                                <label class="layui-form-label">所属发布者</label>
                                <div class="layui-input-inline">
                                    <input type="hidden" name="createBy" lay-verify="orgId"
                                           value="${project.createBy}"/>
                                    <input type="text" id="member_name"
                                           value="${memberForm:getRealNameById(project.createBy)}" class="layui-input"
                                           readonly/>
                                </div>
                                <c:if test="${empty project.id}">
                                    <a class="layui-btn layui-btn-danger" type="button"
                                       onclick="openSelMemberDialog(this,'选择所属合作商','${ctx}/system/member/selectPartner.do','950px','570px')">
                                        选择
                                    </a>
                                </c:if>
                            </div>
                                <div class="layui-form-item">
                                <label class="layui-form-label">项目名称<span class="f-verify-red">*</span></label>
                                <div class="layui-input-block">
                                    <input type="text" name="title" lay-verify="title" placeholder="项目名称"
                                           class="layui-input" value="${project.title}">
                                    <input type="hidden" name="id" value="${copy?'':project.id}"/>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">封面图</label>
                                <div class="cover-content">
                                    <input type="hidden" name="picture" id="pic" lay-verify="picture"
                                           value="${project.picture}"/>
                                    <c:if test="${project == null || empty project.picture}">
                                    <span id="cover-img" class="cover-img"
                                          style="background-image:url(${ctx}/image/posterImg.png)"></span>
                                    </c:if>
                                    <c:if test="${project != null && not empty project.picture}">
                                    <span id="cover-img" class="cover-img"
                                          style="background-image:url('${project.picture}')"></span>
                                    </c:if>
                                    <%--<div class="u-single-upload">--%>
                                    <%--<input type="file" id="upload_single_img" class="u-single-file"--%>
                                    <%--accept="image/gif,image/png,image/jpg,image/jpeg">--%>
                                    <%--<span class="u-single-upload-icon">+添加封面图</span>--%>
                                    <%--</div>--%>
                                    <a class="layui-btn layui-btn-danger" id="sel_pic_img">+添加封面图</a>
                                    <div class="form-word-aux">建议尺寸：800x450</div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">显示封面图<span class="f-verify-red">*</span></label>
                                <div class="layui-input-block">
                                    <input type="radio" name="showPicture" value="1" title="是" lay-verify="required"
                                    ${project == null || project.showPicture == 1 ? 'checked="checked"' : ''}
                                    >
                                    <input type="radio" name="showPicture" value="2" title="否" lay-verify="required"
                                    ${project != null && project.showPicture == 2 ? 'checked="checked"' : ''}
                                    >
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">有反馈通知我<span class="f-verify-red">*</span></label>
                                <div class="layui-input-block">
                                    <input type="radio" name="isRemindMe" value="1" title="是" lay-verify="required"
                                           lay-filter="isRemindMe"
                                    ${project != null && project.isRemindMe == 1 ? 'checked="checked"' : ''}
                                    >
                                    <input type="radio" name="isRemindMe" value="2" title="否" lay-verify="required"
                                           lay-filter="isRemindMe"
                                    ${project == null || project.isRemindMe == 2 ? 'checked="checked"' : ''}
                                    >
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <label class="layui-form-label">是否显示来源<span class="f-verify-red">*</span></label>
                                <div class="layui-input-block">
                                    <input type="radio" ${project != null ? 'disabled':''} name="showResource1" value="1" title="是"
                                    ${project != null && project.showResource == 1 ? 'checked="checked"' : ''}
                                           lay-filter="showResource"
                                    >
                                    <input type="radio" ${project != null ? 'disabled':''}  name="showResource1" value="2" title="否"
                                    ${project == null || project.showResource == 2 ? 'checked="checked"' : ''}
                                           lay-filter="showResource"
                                    >

                                    <input type="hidden" name="showResource" value="${empty project.showResource ? '2' : project.showResource}" />
                                </div>
                            </div>

                            <div id="phoneDiv"
                                 class="layui-form-item" ${project.isRemindMe == 1 ? '' : 'style="display:none"'}>
                                <label class="layui-form-label">手机号<span class="f-verify-red">*</span></label>
                                <div class="layui-input-block" style="width: 190px">
                                    <input type="text" name="phone" maxlength="11" lay-verify="phone" placeholder="手机号"
                                           class="layui-input" value="${project.phone}">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">描述<span class="f-verify-red">*</span></label>
                                <div class="layui-input-block">
                                <textarea name="remarks" class="layui-textarea" lay-verify="remarks"
                                          style="resize:none;height: 100px">${project.remarks}</textarea>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">详情<span class="f-verify-red">*</span></label>
                                <div class="layui-input-block">
                                    <script id="ueditor1" type="text/plain"></script>
                                    <div style="display: none" id="contentView">${project.content}</div>
                                    <input type="hidden" name="content" id="content" lay-verify="content"/>
                                </div>
                            </div>
                        </div>
                    </fieldset>
                    <%@include file="createForm.jsp" %>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <a href="javascript:void(0)" class="layui-btn layui-btn-danger" lay-submit
                               lay-filter="*">立即提交</a>
                            <a href="${ctx}/gatherForm/project/list.do" class="layui-btn layui-btn-primary">取消</a>
                        </div>
                    </div>
                </form>
            </div>
        </div><!--底部-->
        <%@include file="../include/footer.jsp" %>
        <c:if test="${not empty project.createBy}">
            <c:set var="isExpire" value="${memberForm:isExpire(project.createBy, null)}"/>
        </c:if>
    </section>
</div>

<script type="text/javascript" src="${ctxStatic}/UEditor/ueditor.config.js"></script>
<script type="text/javascript" src="${ctxStatic}/UEditor/ueditor.all.js"></script>
<script type="text/javascript" src="${ctxStatic}/UEditor/myplugin/uploadCI.js"></script>
<script type="text/javascript" src="${ctxStatic}/UEditor/myplugin/uploadVideo.js"></script>
<!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
<!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
<script type="text/javascript" src="${ctxStatic}/UEditor/lang/zh-cn/zh-cn.js"></script>
<script>
    var ue = UE.getEditor('ueditor1');

    $(function () {
        var isRemindMeValue = '${project.phone}' || 2;
        layui.use(['form'], function () {
            var form = layui.form, laydate = layui.laydate;

            //自定义验证规则
            form.verify({
                orgId: function (value) {
                    if (!value) {
                        return "请选择所属合作商"
                    }
                    if ('${isExpire}' == 'true' && '${project.id}' == '') {
                        return "当前合作商已到期，不能发布数据"
                    }
                },

                title: function (value) {
                    if (value == "") {
                        return '请填写项目名称';
                    }
                },
                picture: function (value) {
                    var checked = $("[name=showPicture]:checked").val();
                    if ((util.isValid(checked) && checked == "1") && value == "") {
                        return "请上传项目封面图";
                    }
                },
                remarks: function (value) {
                    if (value == "") {
                        return "请填写项目描述";
                    }
                },
                content: function (value) {
                    var content = $("#contentView").html();
                    if (content == "") {
                        return "请填写项目详情";
                    }
                },
                phone: function (value) {
                    if (isRemindMeValue == 1) {
                        if (value == "") {
                            return "请填写手机号";
                        } else if (!util.checkMobile(value)) {
                            return "手机号格式正确";
                        }
                    }
                }
            });

            form.on('radio(isRemindMe)', function (data) {
                isRemindMeValue = data.value;
                if (data.value == 1) {
                    $("#phoneDiv").show();
                } else {
                    $("#phoneDiv").hide();
                }
            });

            form.on('radio(showResource)', function (data) {
                $('[name=showResource]').val(data.value);
            });

            //监听提交
            form.on('submit', function (data) {
                if (checkField()) {
                    txz.submitObject(data.elem, function (callBack) {
                        resetIndex();
                        $("#content").val(ue.getContent().replace(/&quot;/gi, ""));
                        var action = $("#myForm").attr("action");
                        $.post(action, $('#myForm').serialize(), function (res) {
                            typeof callBack === 'function' && callBack();
                            if (res.success) {
                                util.layerMsgSuccess('${copy?"信息复制":""}'+"提交成功", function () {
                                    location.href = "${ctx}/gatherForm/project/list.do";
                                });
                            } else {
                                util.layerMsgError("提交失败")
                            }
                        });
                    });
                    return false;
                }
            });
        });
        ue.addListener('ready', function () {
            this.setHeight(500);
            this.setContent($("#contentView").html());
        });
        ue.addListener('blur', function () {
            $("#contentView").html(ue.getContent());
        });

        $('#sel_pic_img').click(function () {
            txz.openSelImg({
                min: 1,
                max: 1,
                info: '建议尺寸：800x450',
                type: '<%=TargetType.GATHER_FORM.getCode()%>',
                cb: function (imgs) {
                    $('#cover-img').css('background-image', 'url(' + imgs[0].path + ')');
                    $('#pic').val(imgs[0].path);
                }
            })
        })
    })


    // 文本编辑器图片上传
    function uEditorUploadCI(editor) {
        txz.openSelImg({
            min: 1,
            type: '<%=TargetType.GATHER_FORM.getCode()%>',
            cb: function (imgs) {
                if (imgs.length) {
                    for (var i = 0, item; i < imgs.length; i++) {
                        item = imgs[i];
                        editor.focus();
                        editor.execCommand('inserthtml', item.htmlStr);
                    }
                }
            }
        })
    }

    function uEditorUploadCIVideo(editor) {
        txz.openSelVideo({
            min: 1,
            type: '<%=TargetType.GATHER_FORM.getCode()%>',
            cb: function (imgs) {
                if (imgs.length) {
                    for (var i = 0, item; i < imgs.length; i++) {
                        item = imgs[i];
                        editor.focus();
                        editor.execCommand('inserthtml', item.htmlStr);
                    }
                }
            }
        })
    }

    function openSelMemberDialog(that, title, url, width, height) {
        txz.openDialogIframe({
            width: width,
            height: height,
            title: title,
            url: url,
            cb: function (opt) {
                var content = opt.content;
                if (content.doSubmit()) {
                    var data = content.returnData();
                    $('input[name=createBy]').val(data.id);
                    $('#member_name').val(data.name);
                    top.layer.close(opt.index);
                }
            }
        })
    }
</script>
</body>
<script type="text/javascript">
    txz.initHeader({
        nav: [{
            name: '项目管理',
            href: '${ctx}/gatherForm/project/list.do'
        }, {
            name: '${project == null ? '发布' : (copy?'复制':'编辑')}项目',
            curr: true
        }],
        btns: [{
            name: '返回',
            href: 'javascript:history.back();',
            icon: 'back'
        }]
    });
</script>
</html>